from django.contrib import admin
from apiv2.models import User,Card,Book,User_Books,Comment,Plan
from django.utils.safestring import mark_safe
# Register your models here.

class MyAdminSite(admin.AdminSite):
    site_header = '默默记代码管理系统'  # 此处设置页面显示标题
    site_title = '默默记代码'  # 此处设置页面头部标题


admin_site = MyAdminSite(name='management')


@admin.register(User_Books)
class User_BooksAdmin(admin.ModelAdmin):
    list_display = ('id','user','book','res')


@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    def get_img(self,obj):
        picurl = obj.pic
        try:
            img = mark_safe('<img src="{}" width="50px" />'.format(picurl))
        except Exception as e:
            img = picurl
        return img
    get_img.allow_tags = True
    list_display = ('id', 'openid', 'name', 'get_img')
    readonly_fields = ['get_img']

@admin.register(Card)
class CardAdmin(admin.ModelAdmin):
    list_display = ('id','title', 'content', 'level', 'type')
    # list_editable 设置默认可编辑字段
    list_editable = ['title','content','level', 'type']

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ('id','title', 'msg', 'type')


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ('id','card', 'user', 'content', 'likes')


@admin.register(Plan)
class PlanAdmin(admin.ModelAdmin):
    list_display = ('id','user', 'book', 'card', 'lastUpdate','lastLearn','count','point')

    # list_display_links = ['user_name']
    # list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 50

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('user_id',)

    # fk_fields 设置显示外键字段
    # fk_fields = ('card_title','book_title')

    list_filter = ('user', 'book', 'card', 'lastLearn')
    search_fields = ('count', 'point')  # 搜索字段
    date_hierarchy = 'lastUpdate'  # 详细时间分层筛选　
